/**
 * Copyright &copy; 2015-2020 <a href="http://www.jeeplus.org/">JeePlus</a> All rights reserved.
 */
package com.jeeplus.modules.replenishment.service;

import java.util.List;

import com.jeeplus.modules.account.entity.Account;
import com.jeeplus.modules.account.service.AccountService;
import com.jeeplus.modules.centerposition.entity.CenterPosition;
import com.jeeplus.modules.centerposition.service.CenterPositionService;
import com.jeeplus.modules.goodsstore.entity.GoodsStore;
import com.jeeplus.modules.goodsstore.service.GoodsStoreService;
import com.jeeplus.modules.integraldetail.entity.IntegralDetail;
import com.jeeplus.modules.integraldetail.service.IntegralDetailService;
import com.jeeplus.modules.member.entity.Member;
import com.jeeplus.modules.member.service.MemberService;
import com.jeeplus.modules.rewardparameters.entity.RewardParameters;
import com.jeeplus.modules.rewardparameters.service.RewardParametersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.jeeplus.core.persistence.Page;
import com.jeeplus.core.service.CrudService;
import com.jeeplus.modules.replenishment.entity.Replenishment;
import com.jeeplus.modules.replenishment.mapper.ReplenishmentMapper;

/**
 * 补货申请Service
 * @author w
 * @version 2021-09-16
 */
@Service
@Transactional(readOnly = true)
public class ReplenishmentService extends CrudService<ReplenishmentMapper, Replenishment> {

	@Autowired
	private CenterPositionService centerPositionService;
	@Autowired
	private GoodsStoreService goodsStoreService;
	@Autowired
	private AccountService accountService;
	@Autowired
	private MemberService memberService;
	@Autowired
	private IntegralDetailService integralDetailService;
	@Autowired
	private RewardParametersService rewardParametersService;

	public Replenishment get(String id) {
		return super.get(id);
	}
	
	public List<Replenishment> findList(Replenishment replenishment) {
		return super.findList(replenishment);
	}
	
	public Page<Replenishment> findPage(Page<Replenishment> page, Replenishment replenishment) {
		return super.findPage(page, replenishment);
	}
	
	@Transactional(readOnly = false)
	public void save(Replenishment replenishment) {
		super.save(replenishment);
	}
	
	@Transactional(readOnly = false)
	public void delete(Replenishment replenishment) {
		super.delete(replenishment);
	}

	@Transactional(readOnly = false)
	public void apply(Member member, GoodsStore goodsStore, Integer num, String mobile, String address) {
		//积分中心积分收益奖励比例
		Double percentIntegral = 0.0;
		//查询奖励比例
		List<RewardParameters> rewardParametersList = rewardParametersService.findList(new RewardParameters());
		if (rewardParametersList.size()>0){
			percentIntegral = rewardParametersList.get(0).getIntePercentIntegral();
		}
		Double integral = goodsStore.getIntegral() * num;
		Account account = accountService.get(member.getAccount().getId());
		if (account == null){
			throw new RuntimeException("账户信息有误");
		}
		if (account.getIntegral() < integral * (1-percentIntegral)){
			throw new RuntimeException("积分不足");
		}
		if (goodsStore.getStock() < num){
			throw new RuntimeException("积分商品平台库存不足！");
		}
		goodsStore.setStock(goodsStore.getStock() - num);
		goodsStoreService.save(goodsStore);
		account.setIntegral(account.getIntegral() - (integral * (1-percentIntegral)));
		accountService.save(account);

		Replenishment replenishment = new Replenishment();
		replenishment.setMember(member);
		replenishment.setGoodsStore(goodsStore);
		replenishment.setNum(num);
		replenishment.setMobile(mobile);
		replenishment.setAddress(address);
		replenishment.setShipped("0");
		replenishment.setIntegral(integral * (1-percentIntegral));
		replenishment.setRemarks("0");
		super.save(replenishment);

		//记录
		IntegralDetail integralDetail = new IntegralDetail();
		integralDetail.setIntegral(replenishment.getIntegral());
//			integralDetail.setOrderNo(byNo.getOrdersNo());
		integralDetail.setIntegralType("5");
		integralDetail.setMember(member);
		integralDetailService.save(integralDetail);
	}

	@Transactional(readOnly = false)
	public void saveExpress(Replenishment replenishment) {
		//平台库存减少
		GoodsStore goodsStore = goodsStoreService.get(replenishment.getGoodsStore().getId());
		if (goodsStore != null){
			Member member = memberService.get(replenishment.getMember().getId());
			if (member == null){
				throw new RuntimeException("用户信息有误！");
			}
			//平台积分
			List<RewardParameters> rewardParametersList = rewardParametersService.findList(new RewardParameters());
			if (rewardParametersList.size()>0){
				rewardParametersList.get(0).setTotalIntegral(rewardParametersList.get(0).getTotalIntegral() + replenishment.getIntegral());
				rewardParametersService.save(rewardParametersList.get(0));
			}
			//更改状态
			replenishment.setShipped("1");
			super.save(replenishment);
			//加积分中心的持有库存
			CenterPosition centerPosition = new CenterPosition();
			centerPosition.setMember(replenishment.getMember());
			centerPosition.setGoodsStore(replenishment.getGoodsStore());
			List<CenterPosition> list = centerPositionService.findList(centerPosition);
			if (list.size() > 0){
				for (CenterPosition position : list) {
					position.setStock(position.getStock() + replenishment.getNum());
					centerPositionService.save(position);
				}
			}else {
				CenterPosition centerPosition1 = new CenterPosition();
				centerPosition1.setMember(replenishment.getMember());
				centerPosition1.setGoodsStore(replenishment.getGoodsStore());
				centerPosition1.setStock(replenishment.getNum());
				centerPositionService.save(centerPosition1);
			}
		}else {
			throw new RuntimeException("积分商品信息有误,请检查是否已被删除");
		}
	}


	@Transactional(readOnly = false)
	public void cancel(Replenishment replenishment) {
		if ("0".equals(replenishment.getRemarks()) && "0".equals(replenishment.getShipped())){
			replenishment.setRemarks("1");
			super.save(replenishment);
			//返还扣除的积分
			Member member = memberService.get(replenishment.getMember().getId());
			if (member != null){
				Account account = accountService.get(member.getAccount().getId());
				if (account != null){
					account.setIntegral(account.getIntegral() + replenishment.getIntegral());
					accountService.save(account);
				}
			}
			GoodsStore goodsStore = goodsStoreService.get(replenishment.getGoodsStore().getId());
			if (goodsStore != null){
				goodsStore.setStock(goodsStore.getStock() + replenishment.getNum());
				goodsStoreService.save(goodsStore);
			}
		}
	}

	/**
	 * 确认发货
	 * @param replenishment
	 */
	@Transactional(readOnly = false)
	public void confirmSend(Replenishment replenishment) {
		if ("0".equals(replenishment.getRemarks()) && "0".equals(replenishment.getShipped())){
			//查询对应商品
			GoodsStore goodsStore = goodsStoreService.get(replenishment.getGoodsStore().getId());
			if (goodsStore != null){
				Member member = memberService.get(replenishment.getMember().getId());
				if (member == null){
					throw new RuntimeException("用户信息有误！");
				}
				//平台积分
				List<RewardParameters> rewardParametersList = rewardParametersService.findList(new RewardParameters());
				if (rewardParametersList.size()>0){
					rewardParametersList.get(0).setTotalIntegral(rewardParametersList.get(0).getTotalIntegral() + replenishment.getIntegral());
					rewardParametersService.save(rewardParametersList.get(0));
				}
				//更改状态
				replenishment.setShipped("1");
				super.save(replenishment);
				//加积分中心的持有库存
				CenterPosition centerPosition = new CenterPosition();
				centerPosition.setMember(replenishment.getMember());
				centerPosition.setGoodsStore(replenishment.getGoodsStore());
				List<CenterPosition> list = centerPositionService.findList(centerPosition);
				if (list.size() > 0){
					for (CenterPosition position : list) {
						position.setStock(position.getStock() + replenishment.getNum());
						centerPositionService.save(position);
					}
				}else {
					CenterPosition centerPosition1 = new CenterPosition();
					centerPosition1.setMember(replenishment.getMember());
					centerPosition1.setGoodsStore(replenishment.getGoodsStore());
					centerPosition1.setStock(replenishment.getNum());
					centerPositionService.save(centerPosition1);
				}
			}
		}
	}
}